/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.screen;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.screen.BlessWordsDao;
import com.boxin.ims.modules.wechat.entity.screen.BlessWords;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 祝福词Service
 * @author Jakemanse
 * @version 2013-11-01
 */
@Component
@Transactional(readOnly = true)
public class BlessWordsService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(BlessWordsService.class);
	
	@Autowired
	private BlessWordsDao blessWordsDao;
	
	public BlessWords get(Long id) {
		return blessWordsDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-2  下午4:54:07
	 * @function <p> 查询祝福词  </p>
	 * @param blessWords
	 * @return
	 */
	public List<BlessWords> find(BlessWords blessWords){
		DetachedCriteria dc = blessWordsDao.createDetachedCriteria();
		dc.add(Restrictions.eq("screen.id", blessWords.getScreen().getId()));
		dc.add(Restrictions.eq("status", blessWords.getStatus()));
		
		dc.addOrder(Order.desc("id"));
		
		return blessWordsDao.find( dc);
	}
	
	public Page<BlessWords> find(Page<BlessWords> page, BlessWords blessWords) {
		DetachedCriteria dc = blessWordsDao.createDetachedCriteria();
		if (blessWords.getUser()!=null && blessWords.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", blessWords.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(blessWords.getName())){
			dc.add(Restrictions.like("name", "%"+blessWords.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return blessWordsDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(BlessWords blessWords) {
		blessWordsDao.save(blessWords);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		blessWordsDao.deleteById(id);
	}
	
}
